<?php
class Picture extends AppModel{

  /*
   * Gets the array of thumbnails by family ID
   *
   */
  function getThumbsByFamily($fid = null) {
    // Must load the family model in a slightly different way
    $fa = ClassRegistry::init('Family');
	    
    $res = $fa->getEventsFromFamilies(array($fid));	       
    if (empty($res)) {
      return array();
    }
	    
    // Loop through events and create a thumbnail array for each
    $thumbarr = array();
    foreach($res as $evid) {
      $thumbarr[$evid] = $this->getThumbsByEID($evid, 4);
    }
	    
    return $thumbarr;
  }

  /*
   * Gets an array of thumbnails for a given event ID. Will return up to $Num thumbnails
   *
   */
  function getThumbsByEID($EID = null, $Num = 4) {
    $conditions = array('Picture.event_id' => $EID);
    $fields = array('Picture.file_name', 'Picture.file_type');
    $limit = 4;
	    
    $arr = $this->find('all', array('conditions' => $conditions,
				    'fields' => $fields, 'limit' => $limit));
	    
    $thumbarr = array();    
    foreach($arr as $thumb) {
      $thumbarr[] = $thumb['Picture'];
    }
    return $thumbarr;
  }
	
	
  /*
   * Gets all pictures from a given event ID
   * Takes the Event ID and picture array
   * Returns the list of pictures
   */
  function getPicturesFromEvent($eid, $sortArray = array('Picture.id ASC')) {
    $result =  $this->find('all', array('conditions' => array('Picture.event_id' => $eid), 'order' => $sortArray));
    $pictures = array();
    foreach ($result as $pic) {
      $pictures[] = $pic['Picture'];
    }
    return $pictures;
  }

  /*
   * Returns whether the person with the given uid owns the picture with the given pid
   *
   */
  function userOwnsPicture($pid, $uid){
    $result = $this->find('first', array('conditions' => array('Picture.id' => $pid, 'Picture.person_id' => $uid)));
    return !empty($result);
  }
}
?>
